package com.petmall.service;

import com.petmall.entity.Message;
import com.petmall.entity.User;
import com.petmall.mapper.MessageMapper;
import com.petmall.util.MessageUtil;
import com.petmall.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by pig2guang on 2017/5/1.
 */
@Service
public class MessageService {

    @Autowired
    private MessageMapper messagemapper;

    //获取用户所接收的最新一条信息
    public Message getUserLatestMessage(Object user){
        return getUserAllMessage(user).get(0);
    }

    //获取用户所接收的所有信息
    public List<Message> getUserAllMessage(Object user){
        return messagemapper.selectUserAllMessage(new UserUtil().getUserId(user), new MessageUtil().message_receiver_user);
    }

    //获取用户所接收的所有信息
    public List<Message> getAdminAllMessage(){
        return messagemapper.selectAdminAllMessage(new MessageUtil().message_receiver_admin);
    }

    //添加一条欢迎新用户注册信息
    public void addWelcomeMessage(User user){
        messagemapper.insertMessage(new MessageUtil().message_type_welcome, user.getId(), new MessageUtil().message_welcome_info, getCurrentTime(), new MessageUtil().message_receiver_user, "" );
    }

    //添加一条用户提醒发货信息
    public void addOrderPromptMessage(String order_id, Object user){
        long user_id = new UserUtil().getUserId(user);
        String message = "用户（id：" + user_id + "），订单号：" + order_id + "，" + new MessageUtil().message_prompt_info;
        messagemapper.insertMessage(new MessageUtil().message_type_prompt, user_id, message, getCurrentTime(), new MessageUtil().message_receiver_admin, order_id );
    }

    //添加一条管理员提醒用户订单已发货信息
    public void addSendMessage(String order_id, long user_id){
        String message = "您的订单（订单号：" + order_id + "）" + new MessageUtil().message_send_info;
        messagemapper.insertMessage(new MessageUtil().message_type_send, user_id, message, getCurrentTime(), new MessageUtil().message_receiver_user, order_id );
    }

    //添加一条提醒用户评价信息
    public void addReviewMessage(String order_id, Object user){
        long user_id = new UserUtil().getUserId(user);
        String message = "您的订单（订单号：" + order_id + "）" + new MessageUtil().message_review_info;
        messagemapper.insertMessage(new MessageUtil().message_type_review, user_id, message, getCurrentTime(), new MessageUtil().message_receiver_user, order_id );
    }

    //添加一条提醒用户订单已完成信息
    public void addCompleteMessage(String order_id, Object user){
        long user_id = new UserUtil().getUserId(user);
        String message = "您的订单（订单号：" + order_id + "）" + new MessageUtil().message_complete_info;
        messagemapper.insertMessage(new MessageUtil().message_type_complete, user_id, message, getCurrentTime(), new MessageUtil().message_receiver_user, order_id );
    }

    //添加一条提醒用户订单已取消信息
    public void addCancleMessage(String order_id, Object user){
        long user_id = new UserUtil().getUserId(user);
        String message = "您的订单（订单号：" + order_id + "）" + new MessageUtil().message_cancle_info;
        messagemapper.insertMessage(new MessageUtil().message_type_cancle, user_id, message, getCurrentTime(), new MessageUtil().message_receiver_user, order_id );
    }

    //删除一条信息
    public void deleteMessage(String message_id){
        messagemapper.deleteMessage( Long.valueOf(message_id) );
    }

    //获取信息最新修改时间
    private String getCurrentTime(){
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = format.format(date);
        return time ;
    }

}
